package com.ying.core.response;

/**
 * 接口返回对象
 * @author 硬
 * @date 2021/12/04 20:13
 */
public class ApiResponse {

    private Integer code;

    private String msg;

    private Object data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public void setResultCode(ResultCode code) {
        this.code = code.code();
        this.msg = code.message();
    }

    //成功 不返回数据直接返回成功信息
    public static ApiResponse success() {
        ApiResponse result = new ApiResponse();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    //成功 并且加上返回数据
    public static ApiResponse success(Object data) {
        ApiResponse result = new ApiResponse();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    //成功 自定义成功返回状态 加上数据
    public static ApiResponse success(ResultCode resultCode, Object data) {
        ApiResponse result = new ApiResponse();
        result.setResultCode(resultCode);
        result.setData(data);
        return result;
    }

    // 单返回失败的状态码
    public static ApiResponse failure(ResultCode resultCode) {
        ApiResponse result = new ApiResponse();
        result.setResultCode(resultCode);
        return result;
    }

    // 返回失败的状态码 及 数据
    public static ApiResponse failure(ResultCode resultCode, Object data) {
        ApiResponse result = new ApiResponse();
        result.setResultCode(resultCode);
        result.setData(data);
        return result;
    }
}
